package com.unionpay.upyzt.example;

import com.unionpay.upyzt.Upyzt;
import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.LargePayment;
import com.unionpay.upyzt.resp.LargePaymentResp;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;

public class LargePaymentExample {

    private String balanceAcctId;

    public LargePaymentExample(String balanceAcctId) {
        this.balanceAcctId = balanceAcctId;
    }

    public static void runDemos(String balanceAcctId) throws UpyztException {
        LargePaymentExample example = new LargePaymentExample(balanceAcctId);
        // 大额订单号生成
        LargePaymentResp largePaymentResp = example.create();
        // 大额订单号查询（系统订单号）
        largePaymentResp = example.retrieveById(largePaymentResp.getLargePaymentNo());
        // 消费订单查询（平台订单号）
        largePaymentResp = example.retrieveByOutOrderNo(largePaymentResp.getOutOrderNo());
        // 大额订单号撤销
        largePaymentResp = example.cancel(largePaymentResp.getLargePaymentNo());
    }

    private LargePaymentResp create() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("out_order_no", Main.generateRandomOutNo());
        params.put("sent_at", OffsetDateTime.now().toString());
        params.put("amount", 100);
        params.put("recv_balance_acct_id", balanceAcctId);
        params.put("pay_bank_acct_no", Upyzt.encryptField("123456789"));
        params.put("pay_bank_acct_name", Upyzt.encryptField("张三"));
        params.put("contract_imgs", new String[]{"jTpGmg05InE4d2I6_H7I4"});
        params.put("remark", "用于 SDK 示例测试");
        params.put("extra", MockData.extra());
        params.put("metadata", MockData.metadata());

        System.out.println("------- 大额订单号请求 -------");
        LargePaymentResp resp = LargePayment.create(params);
        System.out.println(resp);
        return resp;
    }

    private LargePaymentResp retrieveById(String largePaymentNo) throws UpyztException {
        System.out.println("------- 大额订单号查询（大额订单号） -------");
        LargePaymentResp resp = LargePayment.retrieveById(largePaymentNo);
        System.out.println(resp);
        return resp;
    }

    private LargePaymentResp retrieveByOutOrderNo(String outOrderNo) throws UpyztException {
        System.out.println("------- 大额订单号查询（平台订单号） -------");
        LargePaymentResp resp = LargePayment.retrieveByOutOrderNo(outOrderNo);
        System.out.println(resp);
        return resp;
    }

    private LargePaymentResp cancel(String largePaymentNo) throws UpyztException {
        System.out.println("------- 大额订单号撤销 -------");
        LargePaymentResp resp = LargePayment.cancel(largePaymentNo);
        System.out.println(resp);
        return resp;
    }
}
